//
//  MineViewController.m
//  TimeBoat
//
//  Created by yang on 2022/7/5.
//  Copyright © 2022 yang. All rights reserved.
//

#import "PerfectInformationCtr.h"
#import "BRStringPickerView.h"
#import "BRDatePickerView.h"
#import "PPTextfield.h"

@interface PerfectInformationCtr ()

@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLab;
@property (weak, nonatomic) IBOutlet UILabel *sexLab;
@property (weak, nonatomic) IBOutlet UILabel *birthdayLab;
@property (weak, nonatomic) IBOutlet PPTextfield *heightTextField;
@property (weak, nonatomic) IBOutlet PPTextfield *weightTextField;

@end


@implementation PerfectInformationCtr


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.tag = 10086;
    self.heightTextField.isPrice = YES;
    self.heightTextField.pointNum = 2;
    self.heightTextField.maxAmount = 999.99;
    
    self.weightTextField.isPrice = YES;
    self.weightTextField.pointNum = 2;
    self.weightTextField.maxAmount = 999.99;
    
    [self setUserInfo];
}


- (void)setUserInfo{
    NSString *nicknameStr = [AccountManager shareAccount].loginModel.nickname;
    if (nicknameStr.length == 0) {
        NSString *newTelephone = [self.telephone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        nicknameStr = [NSString stringWithFormat:@"%@",newTelephone];
    }
    self.nickNameLab.text = [nicknameStr isEqualToString:@"(null)"] ? @"" :nicknameStr;;
    
    NSString *avatarUrl = [AccountManager shareAccount].loginModel.avatarUrl;
    
    NSString *gender = [AccountManager shareAccount].loginModel.gender;
    if([gender isEqualToString:@"2"]){
        [self.img sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_girl"]];
        gender = @"女";
    }
    else {
        [self.img sd_setImageWithURL:[NSURL URLWithString:avatarUrl] placeholderImage:[UIImage imageNamed:@"information_icon_boy"]];
        gender = @"男";
    }
    self.sexLab.text = gender;
    
    self.birthdayLab.text = [AccountManager shareAccount].loginModel.birthday;
    
    NSString *height = [AccountManager shareAccount].loginModel.height;
    if (height.length == 0) {
        height = @"170";
    }
    self.heightTextField.text = height;
    
    NSString *weight = [AccountManager shareAccount].loginModel.weight;
    if (weight.length == 0) {
        weight = @"60";
    }
    self.weightTextField.text = weight;
}


- (IBAction)saveClick:(UIButton *)sender {
    //gender性别,2-女/1-男
    NSString *gender = self.sexLab.text;
    if ([gender isEqualToString:@"男"]) {
        gender = @"1";
    }else if([gender isEqualToString:@"女"]){
        gender = @"2";
    }
    NSString *tokenStr = [AccountManager shareAccount].loginModel.token;
    
    NSMutableDictionary *parameter = @{@"avatarUrl":@"",@"birthday":self.birthdayLab.text,@"gender":gender,@"height":self.heightTextField.text,@"nickname":@"",@"token":tokenStr == nil?@"":tokenStr,@"weight":self.weightTextField.text}.mutableCopy;
    [[HttpHelp shareInstance] POSTHTTPV2:API_LOGIN_updateInfo parameters:parameter success:^(NSDictionary * _Nonnull dict) {
        
        NSInteger result = [dict[@"result"] integerValue];
        if (result == 1) {
            [APPDELEGATE configTab];
        }
        
    } failure:^(BOOL netErr, NSString * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}


#pragma mark - 点击性别
- (IBAction)sexClick:(UIButton *)sender {
    [self.view endEditing:YES];
    WeakSelf(self)
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]init];
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    stringPickerView.title = @"";
    stringPickerView.dataSourceArr = @[@"男", @"女"];
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        StrongSelf(weakSelf)
        strongSelf.sexLab.text = resultModel.value;
    };
    [stringPickerView show];
}


#pragma mark - 点击生日
- (IBAction)birthdayClick:(UIButton *)sender {
    [self.view endEditing:YES];
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = BRDatePickerModeYMD;
    datePickerView.selectValue = self.birthdayLab.text;
    __weak typeof(self) wself = self;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
        wself.birthdayLab.text = selectValue;
    };
    [datePickerView show];
}


@end
